﻿namespace MEF_Test.Bricks
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    using MEF_Test.Contracts;

    public class MediaHandlerFactoryProvider : IMediaHandlerFactoryProvider
    {
        #region Constants and Fields

        private readonly Dictionary<string, string> _extensionToContentTypeMappings;

        private readonly Dictionary<string, IMediaHandlerFactory> _mediaHandlersDictionary;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MediaHandlerFactoryProvider"/> class.
        /// </summary>
        /// <param name="mediaHandlersDictionary">The media handlers dictionary.</param>
        /// <param name="extensionToContentTypeMappings">The extension to content type mappings.</param>
        public MediaHandlerFactoryProvider(Dictionary<string, IMediaHandlerFactory> mediaHandlersDictionary, Dictionary<string, string> extensionToContentTypeMappings)
        {
            if (mediaHandlersDictionary == null)
            {
                throw new ArgumentNullException("mediaHandlersDictionary");
            }

            if (extensionToContentTypeMappings == null)
            {
                throw new ArgumentNullException("extensionToContentTypeMappings");
            }

            _mediaHandlersDictionary = mediaHandlersDictionary;
            _extensionToContentTypeMappings = extensionToContentTypeMappings;
        }

        #endregion

        #region Implemented Interfaces

        #region IMediaHandlerFactoryProvider

        /// <summary>
        /// Gets the content type mapped to the specified Path.
        /// </summary>
        /// <param name="path">The path to the file.</param>
        /// <returns>The content type.</returns>
        public string GetContentTypeFromPath(string path)
        {
            var fi = new FileInfo(path);

            if (fi.Attributes.HasFlag(FileAttributes.Directory))
            {
                // it's a directory
                return "folder/local";
            }

            string extension = Path.GetExtension(path);
            if (!string.IsNullOrEmpty(extension) && _extensionToContentTypeMappings.ContainsKey(extension))
            {
                return _extensionToContentTypeMappings[extension];                
            }
            
            return "file/unknown";
        }

        /// <summary>
        /// Gets a media handler factory matching the specified file handler.
        /// </summary>
        /// <param name="fileHandler">The file handler.</param>
        /// <returns>
        /// An <see cref="IMediaHandlerFactory"/> instance which can be used to create <see cref="IMediaHandler"/> instances.
        /// </returns>
        public IMediaHandlerFactory GetMediaHandlerFactory(IFileHandler fileHandler)
        {
            // get the mediahandlers which are known to handle this type of file, based on its content type.            
            return GetMediaHandlerFactory(fileHandler.ContentType);            
        }

        /// <summary>
        /// Factory method to create a MediaHandler.
        /// </summary>
        /// <param name="contentType">the type of the media. See IFileHandler.ContentType</param>
        /// <returns>
        /// An <see cref="IMediaHandlerFactory"/> instance which can be used to create <see cref="IMediaHandler"/> instances.
        /// </returns>
        public IMediaHandlerFactory GetMediaHandlerFactory(string contentType)
        {
            IMediaHandlerFactory retval = null;
            if (_mediaHandlersDictionary.ContainsKey(contentType))
            {
                retval = _mediaHandlersDictionary[contentType];
            }
            return retval;
        }

        #endregion

        #endregion
    }
}